
import axios from 'axios'
axios.defaults.baseURL='/api'
export default function ajax(url,data={},type='get'){
        return new Promise((reslove,reject)=>{
            let promise
            if(type=='get'){
                let dataStr =''
                Object.keys(data).forEach(item=>{
                    dataStr = dataStr + item + '=' +data[item]+'&'
                })
                if(dataStr!==''){
                    dataStr =dataStr.substring(0,dataStr.lastIndexOf('&'))
                    url =url +'?' +dataStr
                }
                
            promise=axios.get(url)
            }else{
            promise=axios.post(url,data)
            }
            promise.then(res=>{
                reslove(res.data)
            }).catch(error=>{
                reject(error)
            })
        })
}